#!/bin/sh
source /root/.profile  # Get Environment Variables from Root Profile

if ! type mono 2>/dev/null; then
    for d in /volume*/@appstore/mono; do
        if [ -x "$d/bin/mono" ]; then
            PATH="$PATH:$d/bin"
            export PATH
            break
        fi
    done
fi

# Package Varables
PACKAGE_NAME_SIMPLE="$(echo "$SYNOPKG_PKGNAME" | awk '{print tolower($0)}' | sed -e 's/ /_/g')"
PACKAGE_DIR="${SYNOPKG_PKGDEST}"
PACKAGE_UPGRADE_FLAG="/tmp/${PACKAGE_NAME_SIMPLE}.upgrade"

# We need more space than what /tmp holds
PACKAGE_TEMP_DIR="${PACKAGE_DIR}/temp"

# These control how the authentication is integrated
SYNO_LOGIN_CGI=/usr/syno/synoman/webman/login.cgi
SYNO_AUTHENTICATE_CGI=/usr/syno/synoman/webman/modules/authenticate.cgi

# If all users should have access, set to "1"
SYNO_ALL_USERS=0

# If we should disable verification of Synology's XSRF tokens, set to "1"
SYNO_SKIP_XSRF=0

# If we should disable Synology auth completely, set to "0"
SYNO_DSM_AUTH=1


# Start & Stop Varables
PID_FILE="/var/run/${PACKAGE_NAME_SIMPLE}.pid"

DaemonStart() {
	DaemonStatus
	if [ $? == 0 ]; then
		echo "Starting ${PACKAGE_NAME_SIMPLE}."

		mkdir -p "${PACKAGE_TEMP_DIR}"

		SYNO_DSM_AUTH=${SYNO_DSM_AUTH} \
		SYNO_SKIP_XSRF=${SYNO_SKIP_XSRF} \
		SYNO_ALL_USERS=${SYNO_ALL_USERS} \
		SYNO_AUTHENTICATE_CGI="${SYNO_AUTHENTICATE_CGI}" \
		SYNO_LOGIN_CGI="${SYNO_LOGIN_CGI}" \
		TMP_DIR="${PACKAGE_TEMP_DIR}" \
		TEMP="${PACKAGE_TEMP_DIR}" \
		mono "${PACKAGE_DIR}/Duplicati.Server.exe" &
		echo $! > "$PID_FILE"		

		cp -f ${PACKAGE_DIR}/dsm.duplicati.conf /usr/local/etc/nginx/conf.d/

	else
		echo "${PACKAGE_NAME_SIMPLE} already running."
	fi
}

DaemonDebug() {
	DaemonStatus
}

DaemonStop() {
	DaemonStatus
	if [ $? == 1 ]; then
		echo "Stopping ${PACKAGE_NAME_SIMPLE}."
		pkill -P $(cat "$PID_FILE")
		rm -f "$PID_FILE"
		
		rm -f /usr/local/etc/nginx/conf.d/dsm.duplicati.conf

		sleep 3
	else
		echo "Nothing to stop for ${PACKAGE_NAME_SIMPLE}."
	fi
}

DaemonStatus() {
	if [ -f "$PID_FILE" ]; then
		PID=$(cat "$PID_FILE")
		
		if [ -n "$(ps | grep $PID | grep -vn "grep $PID")" ]; then
			echo "${PACKAGE_NAME_SIMPLE} is running ..."
			return 1  # is running
		else
			echo "${PACKAGE_NAME_SIMPLE} is NOT running ..."
			rm -f ${PID_FILE}  # Remove Invalid PID
			return 0  # is NOT running
		fi
	else
		echo "${PACKAGE_NAME_SIMPLE} is NOT running ...."
		return 0  # is NOT running
	fi
}

case $1 in
	start)
		DaemonStart
		sleep 1
		DaemonStatus
		exit $(( ! $? ))  # [ $? == 1 ] && exit 0 || exit 1  # this if statement flips the boolean outcome.
	;;
	stop)
		DaemonStop
		sleep 1
		DaemonStatus
		exit $?
	;;
	restart)
		DaemonStop
		sleep 10
		DaemonStart
		sleep 1
		DaemonStatus
		exit $(( ! $? ))  # this if statement flips the boolean outcome.
	;;
	status)
		DaemonStatus
		exit $(( ! $? ))  # this if statement flips the boolean outcome.
	;;
	debug)
		DaemonDebug
		exit 0
	;;
	log-show)
		exit 0
	;;
	log-clear)
		exit 0
	;;
	log)
		exit 0
	;;
	*)
		echo "Usage: $0 {start|stop|restart|status|debug|log|log-show|log-clear}"
		exit 1
	;;
esac
